package Test;

public class Client {
    public static void main(String[] args) {
        String input = "../a/b/../c/./d";
        System.out.println(simplifyPath(input));
        System.out.println(change(10, "1000", 7));
    }

    public static String simplifyPath(String input) {
        input = input.replace("/./", "/");
        String[] items = input.split("/");
        StringBuilder sb = new StringBuilder();
        for (int index = items.length - 1; index >= 0; index--) {
            if (items[index].equals("..") || items[index].equals("")) {
                index--;
            } else {
                sb.append(items[index]);
                sb.append("/");
            }
        }
        return sb.reverse().toString();
    }

    public static String change(int fromBase, String number, int toBase) {
        String[] items = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "a", "b", "c", "d", "e", "f"
        };
        int x = 0;
        String[] fromItems = new String[fromBase];
        String[] toItems = new String[toBase];
        for (int i = 0; i < fromBase; i++) {
            fromItems[i] = items[i];
        }
        for (int i = 0; i < toBase; i++) {
            toItems[i] = items[i];
        }
        if (fromBase > 16 || fromBase < 2 || toBase < 2 || toBase > 16) {
            System.out.println("禁止越界");
        } else {
            for (int i = number.length() - 1; i >= 0; i--) {
                x += power(fromBase, number.length() - 1 - i) * getIndex(fromItems, String.valueOf(number.charAt(i)));
            }
            System.out.println(x);
        }
        StringBuilder sb = new StringBuilder();
        do {
            sb.append(toItems[x % toBase]);
            System.out.println(x + ":" + x % toBase);
            x = x / toBase;
        } while (x != 0);

        return sb.reverse().toString();
    }

    public static int getIndex(String[] arr, String value) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(value)) {
                return i;
            }
        }
        return -1;
    }

    public static int power(int a, int b) {
        int power = 1;
        for (int c = 0; c < b; c++)
            power *= a;
        return power;
    }
}
